/**
 * Urban Parks
 *
 * Team M&M's:
 * Michael Satran Jr
 * Michael Pogson
 * Stephen Hunter
 *
 * Date: Feb 16, 2012
 */
package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import model.Job;
import model.Park;
import model.ParkManager;
import model.Date;

import controller.Controller;

/**
 * @author Michael Satran Jr
 *
 */
public class JobRequestPanel {
	
	private JFrame my_frame;
	private Controller my_controller;
	
	private JComboBox parks;
	
	private JTextField startYear = new JTextField(4);
	private JTextField startMonth = new JTextField(2);
	private JTextField startDay = new JTextField(2);
	
	private JTextField endYear = new JTextField(4);
	private JTextField endMonth = new JTextField(2);
	private JTextField endDay = new JTextField(2);
	
	private JComboBox difficulty = new JComboBox(Job.DIFFICULTY_LEVELS);
	private JTextArea description = new JTextArea();
	
	
	public JobRequestPanel(Controller the_controller, JFrame the_frame) {
		my_controller = the_controller;
		my_frame = the_frame;
		
		List<Park> parks_list = (List<Park>) ((ParkManager)my_controller.getCurrentUser()).getParks();
		String[] parkNames = new String[parks_list.size()];
		int i = 0;
		for (Park offset : parks_list) {
			parkNames[i++] = offset.getParkName();
		}
		
		parks = new JComboBox(parkNames);
	}
	
	public void setup() {
		
		JLabel parkLabel = new JLabel("Park: ");
		JLabel startLabel = new JLabel("Start Date: ");
		JLabel endLabel = new JLabel("End Date: ");
		JLabel difficultyLabel = new JLabel("Job Difficulty: ");
		JLabel descriptionLabel = new JLabel("Job Description: ");
		
		JPanel parkPanel = new JPanel();
		parkPanel.add(parkLabel);
		parkPanel.add(parks);
		
		JPanel startDate = new JPanel(new GridLayout(2, 4));
		startDate.add(new JLabel(""));
		startDate.add(new JLabel("Month (MM)"));
		startDate.add(new JLabel("Day (DD)"));
		startDate.add(new JLabel("Year (YYY)"));
		
		startDate.add(startLabel);
		startDate.add(startMonth);
		startDate.add(startDay);
		startDate.add(startYear);
		
		JPanel endDate = new JPanel(new GridLayout(2, 4));
		endDate.add(new JLabel(""));
		endDate.add(new JLabel("Month (MM)"));
		endDate.add(new JLabel("Day (DD)"));
		endDate.add(new JLabel("Year (YYY)"));
		
		endDate.add(endLabel);
		endDate.add(endMonth);
		endDate.add(endDay);
		endDate.add(endYear);
		
		JPanel difficultyPanel = new JPanel();
		difficultyPanel.add(difficultyLabel);
		difficultyPanel.add(difficulty);
		
		JPanel descriptionPanel = new JPanel(new BorderLayout());
		descriptionPanel.add(descriptionLabel, BorderLayout.WEST);
		descriptionPanel.add(description, BorderLayout.CENTER);
		
		JButton submitJobButton = new JButton("Create Job");
		submitJobButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					
					String parkName = (String)parks.getSelectedItem();
					
					int strtMonth = Integer.parseInt(startMonth.getText());
					int strtDay = Integer.parseInt(startDay.getText());
					int strtYear = Integer.parseInt(startYear.getText());
					
					int eMonth = Integer.parseInt(endMonth.getText());
					int eDay = Integer.parseInt(endDay.getText());
					int eYear = Integer.parseInt(endYear.getText());
					
					int jobDifficulty = Integer.parseInt(Character.toString(((String)difficulty.getSelectedItem()).charAt(0)));
					
					String jobDescription = description.getText();
					
					Date strtDate = new Date(strtMonth, strtDay, strtYear);
					Date eDate = new Date(eMonth, eDay, eYear);
					Job new_job = new Job(strtDate, eDate, jobDescription, jobDifficulty);
					new_job.setParkName(parkName);
					
					my_controller.addJob(new_job, parkName);
					my_frame.dispose();
					
				} catch (NumberFormatException event) {
					JOptionPane.showMessageDialog(null, "Invalid date!");
				} catch (Exception event) {
					JOptionPane.showMessageDialog(null, event.getMessage());
				}
				
			}
			
		});
		
		JPanel main = new JPanel();
		main.setLayout(new BoxLayout(main, BoxLayout.Y_AXIS));
		main.add(parkPanel);
		main.add(startDate);
		main.add(endDate);
		main.add(difficultyPanel);
		main.add(descriptionPanel);
		main.add(submitJobButton);
		main.setPreferredSize(new Dimension(310, 250));
		my_frame.setContentPane(main);
		my_frame.pack();
		my_frame.setVisible(true);
	}
}
